//
//  ReviewsViewController.m
//  CenturyInterconnection
//
//  Created by 郭先生 on 16/4/10.
//  Copyright © 2016年 Shanghai DuRui Information Technology Company. All rights reserved.
//

#import "ReviewsViewController.h"
#import "AvatarBrowser.h"
#import "EditReviewsViewController.h"
#import "CommodityDetailsTableViewController.h"

@interface ReviewsViewController ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *imageArr;
@property (nonatomic,strong) EditReviewsViewController *editReviewsVC;
@property (nonatomic,strong) UIImageView *commentImageView;
@property (nonatomic,strong) NSString *StarStr;

@property (nonatomic,strong) NSMutableArray *allDatas;

@end

@implementation ReviewsViewController

#pragma mark =============== 懒加载 ===============
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.delegate   = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.allDatas=[NSMutableArray array];
    self.imageArr=[NSMutableArray array];
    [self.view addSubview:self.tableView];
    self.navigationItem.title = @"REVIEWS";
    [self getDatas];
    [self addBarButton];
}

#pragma mark 获取评论数据
-(void)getDatas{
    //http://viprunningonline.com/api/index.php?main_page=product_reviews
    
    NSString*urlStr=[NSString stringWithFormat:@"%@",HTTP_ADDRESS];
    NSDictionary*params=@{@"main_page":@"product_reviews",@"products_id":self.productId};
    HttpManager*manager=[[HttpManager alloc]init];
    
    [manager getDataFromNetworkWithUrl:urlStr parameters:params compliation:^(id data, NSError *error) {
        
        NSString*number=[NSString stringWithFormat:@"%@",data[@"status"]];
        
        if ([number isEqualToString:@"1"]) {
            self.allDatas=data[@"data"][@"reviews_list"];
            for (int i=0; i<self.allDatas.count; i++) {
                [self.imageArr addObject:self.allDatas[i][@"reivews_images"]];
            }
        }
        [self.tableView reloadData];
    }];
}

#pragma mark =============== 导航栏按钮 ===============
-(void)addBarButton{
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton * editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton * deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame=CGRectMake(0, 0, ACTUAL_WIDTH(30), ACTUAL_HEIGHT(30));
    editButton.frame = CGRectMake(0, 0, ACTUAL_HEIGHT(30), ACTUAL_HEIGHT(30));
    deleteButton.frame = CGRectMake(0, 0, ACTUAL_WIDTH(30), ACTUAL_HEIGHT(30));
    //    leftButton.backgroundColor = [UIColor redColor];
    //    editButton.backgroundColor = [UIColor yellowColor];
    //    deleteButton.backgroundColor = [UIColor orangeColor];
    
    [leftButton setBackgroundImage:[UIImage imageNamed:@"nav_back"] forState:0];
    [editButton setImage:[UIImage imageNamed:@"nav_edit"] forState:0];
    [deleteButton setImage:[UIImage imageNamed:@"nav_close"] forState:0];
    
    [leftButton addTarget:self action:@selector(touchButton) forControlEvents:UIControlEventTouchUpInside];
    [editButton addTarget:self action:@selector(editView) forControlEvents:UIControlEventTouchDown];
    [deleteButton addTarget:self action:@selector(deleteView) forControlEvents:UIControlEventTouchDown];
    
    UIBarButtonItem*item0=[[UIBarButtonItem alloc]initWithCustomView:leftButton];
    UIBarButtonItem*item1 = [[UIBarButtonItem alloc]initWithCustomView:editButton];
    UIBarButtonItem *item2= [[UIBarButtonItem alloc]initWithCustomView:deleteButton];
    
    self.navigationItem.leftBarButtonItem = item0;
    self.navigationItem.rightBarButtonItems = @[item2,item1];
    
}

-(void)deleteView{
    [self.navigationController popViewControllerAnimated:NO];
}

-(void)editView{
    EditReviewsViewController * ervc = [[EditReviewsViewController alloc]init];
    ervc.productID=[NSNumber numberWithFloat:[self.productId floatValue]];
    [self.navigationController pushViewController:ervc animated:NO];
}

-(void)touchButton{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark =============== 三个方法 ===============
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allDatas.count;
//    return 10;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
        //背景View
        UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, ACTUAL_HEIGHT(200))];
        bgView.tag=11;
        [cell.contentView addSubview:bgView];
        
        //日期
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(ACTUAL_HEIGHT(25), ACTUAL_WIDTH(15), ACTUAL_WIDTH(300), ACTUAL_HEIGHT(20))];
        label.tag=12;
        label.font=[UIFont systemFontOfSize:14];
        label.textColor=RGBACOLOR(151, 152, 153, 1);
        [bgView addSubview:label];
        
        //评论
        UILabel * label2 = [[UILabel alloc]initWithFrame:CGRectMake(ACTUAL_HEIGHT(25),ACTUAL_HEIGHT(65), ACTUAL_WIDTH(300), ACTUAL_HEIGHT(125))];
        label2.tag=13;
        label2.font=[UIFont systemFontOfSize:13];
        label2.textColor=[UIColor blackColor];
        label2.numberOfLines=0;
        [bgView addSubview:label2];
        
        
        UIView *viewLine=[[UIView alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(25), ACTUAL_HEIGHT(198), ACTUAL_WIDTH(325), ACTUAL_HEIGHT(2))];
        viewLine.backgroundColor=RGBACOLOR(227, 228, 229, 1);
        viewLine.tag = 16;
        [cell addSubview:viewLine];
    }
        
    UIView *bgView=[cell viewWithTag:11];
 
    //
    UILabel *label=[cell viewWithTag:12];
    label.text=self.allDatas[indexPath.row][@"date_added"];
    
    //
    UILabel *label2=[cell viewWithTag:13];
    label2.text=self.allDatas[indexPath.row][@"reviews_text"];
  
    //星星
    UIButton *StarBtn1=[[UIButton alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(215), ACTUAL_HEIGHT(40), ACTUAL_WIDTH(20), ACTUAL_HEIGHT(20))];
    [StarBtn1 setImage:[UIImage imageNamed:@"img_star"] forState:UIControlStateSelected];
    [StarBtn1 setImage:[UIImage imageNamed:@"img_star_grey"] forState:0];
    [bgView addSubview:StarBtn1];
    
    UIButton *StarBtn2=[[UIButton alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(240), ACTUAL_HEIGHT(40), ACTUAL_WIDTH(20), ACTUAL_HEIGHT(20))];
    [StarBtn2 setImage:[UIImage imageNamed:@"img_star"] forState:UIControlStateSelected];
    [StarBtn2 setImage:[UIImage imageNamed:@"img_star_grey"] forState:0];
    [bgView addSubview:StarBtn2];
    
    UIButton *StarBtn3=[[UIButton alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(265), ACTUAL_HEIGHT(40), ACTUAL_WIDTH(20), ACTUAL_HEIGHT(20))];
    [StarBtn3 setImage:[UIImage imageNamed:@"img_star"] forState:UIControlStateSelected];
    [StarBtn3 setImage:[UIImage imageNamed:@"img_star_grey"] forState:0];
    [bgView addSubview:StarBtn3];
    
    UIButton *StarBtn4=[[UIButton alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(290), ACTUAL_HEIGHT(40), ACTUAL_WIDTH(20), ACTUAL_HEIGHT(20))];
    [StarBtn4 setImage:[UIImage imageNamed:@"img_star"] forState:UIControlStateSelected];
    [StarBtn4 setImage:[UIImage imageNamed:@"img_star_grey"] forState:0];
    [bgView addSubview:StarBtn4];
    
    UIButton *StarBtn5=[[UIButton alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(315), ACTUAL_HEIGHT(40), ACTUAL_WIDTH(20), ACTUAL_HEIGHT(20))];
    [StarBtn5 setImage:[UIImage imageNamed:@"img_star"] forState:UIControlStateSelected];
    [StarBtn5 setImage:[UIImage imageNamed:@"img_star_grey"] forState:0];
    [bgView addSubview:StarBtn5];
    
   
    self.StarStr=self.allDatas[indexPath.row][@"reviews_rating"];
    
    self.StarStr=@"4";
    if ([self.StarStr isEqualToString:@"1"]) {
        StarBtn1.selected=YES;
    }else if ([self.StarStr isEqualToString:@"2"]){
        StarBtn1.selected=YES;
        StarBtn2.selected=YES;
    }else if ([self.StarStr isEqualToString:@"3"]){
        StarBtn1.selected=YES;
        StarBtn2.selected=YES;
        StarBtn3.selected=YES;
    }else if ([self.StarStr isEqualToString:@"4"]){
        StarBtn1.selected=YES;
        StarBtn2.selected=YES;
        StarBtn3.selected=YES;
        StarBtn4.selected=YES;
    }else if ([self.StarStr isEqualToString:@"5"]){
        StarBtn1.selected=YES;
        StarBtn2.selected=YES;
        StarBtn3.selected=YES;
        StarBtn4.selected=YES;
        StarBtn5.selected=YES;
    }
    
//    //评论图片
//    NSArray *imageArr1=[NSArray array];
//    imageArr1 = @[@"rightbar_contact",@"rightbar_contact",@"rightbar_contact",@"rightbar_contact",@"rightbar_contact"];
//    
//    for (int i = 0; i<imageArr1.count; i++) {
//        _commentImageView=[[UIImageView alloc]init];
//        _commentImageView.backgroundColor=[UIColor greenColor];
//        
//        _commentImageView.frame=CGRectMake(i*65+25, 130, 60, 60);
//        [_commentImageView setImage:[UIImage imageNamed:_imageArr[i]]];
//        
//        _commentImageView.userInteractionEnabled = YES;
//        [_commentImageView setMultipleTouchEnabled:YES];
//        //添加点击操作
//        UITapGestureRecognizer *tap  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(magnifyImage)];
//        tap.delegate = self;
//        [_commentImageView addGestureRecognizer:tap];
//        [bgView addSubview:_commentImageView];
//    }
    
    return cell;
}

//图片放大
- (void)magnifyImage
{
    NSLog(@"放大");
    [AvatarBrowser showImage:self.commentImageView];//调用方法
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
@end